/*
 * @Descripttion: 浏览器兼容工具类
 * @Author: chendingfengmail@163.com
 * @version: 
 * @Date: 2021-07-16 13:44:39
 * @LastEditors: chendingfengmail@163.com
 * @LastEditTime: 2022-01-10 15:41:59
 */
const compatibility = {}

// 浏览器内核
let browser = {
  kernel: function () {
    let u = navigator.userAgent;
    let versions = {
      trident: u.indexOf('Trident') > -1, //IE内核
      presto: u.indexOf('Presto') > -1, //opera内核
      webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
      gecko: u.indexOf('Firefox') > -1, //火狐内核Gecko
    }
    for (const key in versions) {
      if (versions[key]) {
        return key;
      }
    }
  }()
}

/**
 * 时间字段的兼容处理
 * @param {指定处理的时间字符串} date 
 */
compatibility.handleDate = function (date) {
  let distDate = {};
  switch (browser.kernel) {

    // IE
    case 'trident':
      distDate = new Date(Date.parse(date.replace(/-/g, "/")))
      break;
    // 谷歌/苹果
    case 'webKit':
      distDate = new Date(date)
      break;

    default:
      distDate = new Date(date)
      break;
  }
  return distDate;
}

/**
 * 获取时间毫秒数
 * @param {指定处理的时间字符串} date 
 */
compatibility.getDateTime = function (date) {

  let distDateTime = 0;
  switch (browser.kernel) {
    // IE
    case 'trident':
      distDateTime = Date.parse(date.replace(/-/g, "/"))
      break;
    // 谷歌/苹果
    case 'webKit':
      distDateTime = new Date(date).getTime();
      break;

    default:
      distDateTime = new Date(date).getTime();
      break;
  }
  return distDateTime;
}

/**
 * 获取当前内核版本
 */
compatibility.getBrowserKernelVersion = function () {
  return browser.kernel;
}

export default compatibility;
